// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var leftMenu = require("../leftMenu/leftMenu");
	var css_detail = require("./bailReturnDetail.css");
	var tools = require("tools");
	var bailReturnInfoId;
	var pickupId;
	var init = function(page) {
		bailReturnInfoId = page.params.bailReturnInfoId;
		inquireBailReturnDetail(bailReturnInfoId);
		inquireCurrentOperationList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		//营运商务
		$("#bailReturnDetail .operationBusinessAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnOperationBusiness");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//营运总监
		$("#bailReturnDetail .chiefOperationAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnChiefOperation");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//财务
		$("#bailReturnDetail .financialAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnFinancial");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//领导
		$("#bailReturnDetail .leaderAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnLeader");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//总裁
		$("#bailReturnDetail .ceoAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnCeo");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
		
		//现金会计
		$("#bailReturnDetail .accountingAudit .button-Yes").unbind("click").click(function(){
			$(this).parents(".supplementOpinion").attr("action", "auditBailReturnAccounting");
			databind.submitDom($("#bailReturnDetail .supplementOpinion"), function(){
				base.alert(1, "保存成功！");
				router.route("financing/bailReturnDetail.html?bailReturnInfoId=" + bailReturnInfoId);
			});
		});
	};
	
	//权限控制
    var inquireCurrentOperationList = function(){
        base.fnAjax({
            action: 'inquireCurrentOperationList.json',
            async:false,
            fnSuccess: function (data) {
                if (data.status == 200) {
                    for(var i=0;i<data.datas.length;i++){
                        var permission = data.datas[i];
                        var permissionId = permission.permissionId;
                        $.each($("#bailReturnDetail").find("input"), function() {
                        	var $this = $(this);
                        	if($this.attr("permissionId") == permissionId){
                        		$this.removeClass("none");
                        	}
                        });
                    }
                } else {
                    base.alert(3, data.message);
                }
            }
        });
    };
    
    //保证金退还信息详情
	var inquireBailReturnDetail = function(bailReturnInfoId) {
		base.fnAjax({
			action: "inquireBailReturnDetail.json",
			oData: {
				bailReturnInfoId: bailReturnInfoId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#bailReturnDetail"), data.datas[0]);
					if (data.datas[0].pickupId != null && data.datas[0].pickupId != 0) {
						inquirePickupApplicationDetail(data);
					} else {
						$("#bailReturnDetail .pickupInfo").remove();
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//提货申请明细
	var inquirePickupApplicationDetail = function(bailReturnData) {
		base.fnAjax({
			action: "inquireNormalPickupApplicationDetail.json",
			oData: {
				pickupId: bailReturnData.datas[0].pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#bailReturnDetail"), data.datas[0]);
					databind.fillForm($("#bailReturnDetail"), bailReturnData.datas[0]);
					if ($("#pickupDetailDeliveryMethod").html() == 2) {
						$("#pickupDetailDeliveryMethod").html("到港");
					} else if ($("#pickupDetailDeliveryMethod").html() == 1) {
						$("#pickupDetailDeliveryMethod").html("到门");
					}

					if ($("#pickupDetailPickupMethod").html() == 1) {
						$("#pickupDetailPickupMethod").html("现金提货");
					} else if ($("#pickupDetailPickupMethod").html() == 2) {
						$("#pickupDetailPickupMethod").html("换货");
					} else {
						$("#pickupDetailPickupMethod").html("换箱");
					}

					juicer.register("getValueByKey", constant.getValueByKey);

					var tpl1 = $("#pickupDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#pickupDetailBulkstockBody").html(html1);

					var tpl2 = $("#pickupDetailInventoryTpl").html();
					var html2 = juicer(tpl2, data);
					$("#pickupDetailInventoryBody").html(html2);
					
					var tpl3 = $("#exchangeBulkstockTpl").html();
					var html3 = juicer(tpl3, data);
					$("#exchangeBulkstockTable").html(html3);
					
					var tpl4 = $("#exchangeInventoryTpl").html();
					var html4 = juicer(tpl4, data);
					$("#exchangeInventoryTable").html(html4);
					
					var tpl5 = $("#exchangeBulkstockInfoTpl2").html();
					var html5 = juicer(tpl5, data);
					$("#exchangeBulkstockInfoBody2").html(html5);
					
					var fs = data.datas[0].fileUuids.split(",");
					for (var j = 0; j < fs.length; j++) {
						if ($.trim(fs[j]) != "") {
							if (tools.inquireFile(fs[j]) != "") {
								$("#pickupDetail .fs").append('<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + fs[j] + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + fs[j] + '" class="showProductIntro">' + tools.inquireFile(fs[j]) + '</a></div> ');
							}
						}
					}

					if ($("#pickupDetailInventoryBody").find("tr").length == 1) {
						$("#pickupDetailInventoryBody").parent().addClass("none");
					}
					if ($("#pickupDetailBulkstockBody").find("tr").length == 1) {
						$("#pickupDetailBulkstockBody").parent().addClass("none");
					}
					if ($("#exchangeInventoryTable").find("tr").length == 1) {
						$("#exchangeInventoryTable").parent().addClass("none");
					}
					if ($("#exchangeBulkstockTable").find("tr").length == 1) {
						$("#exchangeBulkstockTable").parent().addClass("none");
					}

					$("#pickupDetailInventoryBody").on("click", ".detail", function() {
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});
					
					if (data.datas[0].exchangeStatus == 1) {
						$("#consentPickup").html("评估");
					} else if (data.datas[0].exchangeStatus == 2) {
						$("#oppasePickup").remove();
						$("#consentPickup").remove();
						$("#exchangeBulkstockInfoBody2").removeClass("none");
					} else if (data.datas[0].exchangeStatus == 3) {
						$("#consentPickup").html("协议上传").attr("id", "protocol");
						$("#oppasePickup").remove();
					}
					
					$.each($(".auditInfo .data"), function() {
						var $this = $(this);
						if ($.trim($this.html()).length == 0) {
							$this.parents(".auditInfo").remove();
						}
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});